Description

The following are the steps to configure a basic validation using Webwork.

Step 1:

Write up a html form.

<html>
<head>
    <title>Validation - Basic</title>
    <ww:head/>
</head>

<body>

<p/>

<p>
The following form uses labelposition="left"
<ww:form id="f0" method="post" labelposition="left">
    <ww:textfield label="Name" name="name" labelposition="left"/>
    <ww:textfield label="Age" name="age" labelposition="left"/>
    <ww:textfield label="Favorite color" name="answer" labelposition="left"/>
    <ww:submit/>
</ww:form>
</p>

<p>
The following form uses labelposition="top"
<ww:form id="f1" method="post" labelposition="top">
    <ww:textfield label="Name" name="name" labelposition="top"/>
    <ww:textfield label="Age" name="age" labelposition="top"/>
    <ww:textfield label="Favorite color" name="answer" labelposition="top"/>
    <ww:submit/>
</ww:form>
</p>

</body>
</html>

Step 2:

Write up the action class. Get methods must be supplied for validation to work.

public class QuizAction extends ActionSupport {
    String name;
    int age;
    String answer;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAnswer() {
        return answer;
    }

    public void setAnswer(String answer) {
        this.answer = answer;
    }
}

Step3:

Write up the validators to be used. The validation.xml format is either <ActionClassName>-validation.xml or <ActionClassName>-<ActionContextName>-validation.xml

<!--
    Add the following DOCTYPE declaration as first line of your XXX-validation.xml file:
    <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
-->
<validators>
    <field name="name">
        <field-validator type="requiredstring">
            <message key="validation.name.required"/>
        </field-validator>
    </field>
    <field name="age">
        <field-validator type="int">
            <param name="min">13</param>
            <param name="max">19</param>
            <message key="validation.age.invalid" />
        </field-validator>
    </field>
</validators>